package com.joker.springboottestdemo.job;

import com.joker.demo.core.annotation.JobHandle;
import com.joker.demo.core.domain.dto.JobExecutorParamsDTO;
import com.joker.demo.core.domain.dto.JobExecutorResultDTO;
import com.joker.demo.core.executor.JobExecutorService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 延迟任务测试服务
 * 
 * @author feixiang.li
 * @since 2025-01-27
 */
@Service
//@JobHandle("delayTestJob")
public class DelayJobService implements JobExecutorService {

    private static final Logger log = LoggerFactory.getLogger(DelayJobService.class);

    @Override
    public JobExecutorResultDTO execute(JobExecutorParamsDTO params) {
        String currentTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        
        log.info("延迟任务开始执行，当前时间: {}, 分片参数: {}, 分片索引: {}", 
                currentTime, params.getShardingParam(), params.getShardingParamIndex());
        
        // 模拟任务执行
        try {
            Thread.sleep(1000); // 模拟1秒执行时间
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        
        String result = String.format("延迟任务执行完成，执行时间: %s, 分片参数: %s, 分片索引: %d", 
                currentTime, params.getShardingParam(), params.getShardingParamIndex());
        
        log.info(result);
        
        JobExecutorResultDTO response = new JobExecutorResultDTO();
        response.setResult(result);
        return response;
    }
} 